package csel.controller.decoders;

import csel.controller.CommandDummy;
import csel.controller.dispatchers.AbstractDispatcher;
import csel.controller.factories.CommandFactory;
import csel.controller.factories.ModelCommandFactory;
import csel.controller.factories.ViewCommandFactory;
import csel.controller.listeners.KeyboardListener;

/**
 * Reasons for Existence: Decodes keypresses, selects a dispatcher and command
 * factory 
 * Team Members Responsible: Tait 
 * Implementation: Nik, Tait 
 * Testing: Nik, Tait
 * 
 * @author Nikolas Wolfe
 */
public class KeyEventDecoder extends EventDecoder
{
	private KeyboardListener listener;
	private String keyCommand;
	private boolean valid;

	//constructor
	public KeyEventDecoder(KeyboardListener listener,
			AbstractDispatcher modelDispatcher,
			AbstractDispatcher viewDispatcher,
			ModelCommandFactory modelCommandFactory,
			ViewCommandFactory viewCommandFactory)
	{
		this.listener = listener;
		this.modelDispatcher = modelDispatcher;
		this.viewDispatcher = viewDispatcher;
		this.modelCommandFactory = modelCommandFactory;
		this.viewCommandFactory = viewCommandFactory;
	}

	//overrides superclass and retrieves the event from the listener
	@Override
	protected void retrieveCommand()
	{
		System.out.println("[CONTROLLER] " + toString() + " is retrieving the command!");
		keyCommand = listener.getStringEvent();
	}

	//overrides superclass and sends the created command to the dispatch queue
	@Override
	protected void sendCommand() throws InterruptedException
	{
		if (valid)
		{
			// export the command to the dispatch queue
			this.currentDispatcher.export(this.gameCommand);
			System.out.println("[CONTROLLER] " + toString()
					+ " thread dispatched " + "a command to the model.");
		}
	}

	//overrides superclass and creates the command for the given event
	@Override
	protected void createCommand()
	{
		if (valid)
		{
			System.out.println("[CONTROLLER] " + toString() + " is creating the command!");
			this.gameCommand = this.currentCommandFactory.createGameCommand();
		}
	}

	//overrides superclass and parses the event and sets the creation of the command in motion
	@Override
	public void parseCommand()
	{
		valid = false;
		if (keyCommand.startsWith("p"))
		{
			if (keyCommand.endsWith("1") || keyCommand.endsWith("2")
					|| keyCommand.endsWith("3") || keyCommand.endsWith("4")
					|| keyCommand.endsWith("6") || keyCommand.endsWith("7")
					|| keyCommand.endsWith("8") || keyCommand.endsWith("9"))
			{
				valid = true;
				System.out.println("[CONTROLLER] Number " + keyCommand
						+ " was pressed");
				this.setToModel();
				modelCommandFactory.newMoveCommandFactory(new Character(
						listener.getKeyEvent().getKeyChar()));
			} else if ((keyCommand.endsWith("q")))
			{
				valid = true;
				System.out.println("[CONTROLLER] q Key was pressed");
				this.setToModel();
				modelCommandFactory.newQuitCommandFactory();
			} else
			{
				System.out.println("[CONTROLLER] Key " + keyCommand
						+ " is not a valid command");
			}
		} else if (keyCommand.startsWith("r"))
		{
			if (keyCommand.endsWith("1") || keyCommand.endsWith("2")
					|| keyCommand.endsWith("3") || keyCommand.endsWith("4")
					|| keyCommand.endsWith("6") || keyCommand.endsWith("7")
					|| keyCommand.endsWith("8") || keyCommand.endsWith("9"))
			{
				valid = true;
				System.out.println("[CONTROLLER] Number " + keyCommand
						+ " was released");
				this.setToModel();
				modelCommandFactory.newStopCommandFactory();
			} else
			{
				System.out.println("[CONTROLLER] Key " + keyCommand
						+ " is not a valid command");
			}
		}
	}

}
